<td><button class="btn btn-primary" id="update_gonghai_Button_{{ kehu.id }}" data-toggle="modal" data-target="#updateCustomerModal_{{ kehu.id }}"> 领取</button>

<!-- Modal -->
<div class="modal fade" id="updateCustomerModal_{{ kehu.id }}" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
  <div class="modal-dialog modal-dialog-centered" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalCenterTitle">领取客户</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body" >
        <form id="updateCustomerForm_{{ kehu.id }}">
        {% csrf_token %}
{#            {{ KeHu_zhuangonghai_Form }}#}
<div id="id_if_gonghai" class="radio"><div>
    <label for="id_if_gonghai_1"><input type="radio" name="if_gonghai" value="0" class="radio" required="" id="id_if_gonghai_1" checked>
 领取</label>
</div>
</div>
        {{ form }}
        </form>
          <div id="message_{{ kehu.id }}" role="alert"></div>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">取消</button>
        <button type="button" class="btn btn-primary" id="updateCustomerConfirmButton_{{ kehu.id }}">点击领取</button>
      </div>
    </div>
  </div>
</div>


<script>
    $(document).ready(function() {
  // 绑定点击事件到更新按钮
    $('#update_gonghai_Button_{{ kehu.id }}').click(function() {
    // 清空模态框中的表单字段（如果有的话）
    $('#updateCustomerForm_{{ kehu.id }}')[0].reset();
    // 显示模态框
    $('#updateCustomerModal_{{ kehu.id }}').modal('show');
  });
  // 绑定点击事件到模态框中的“Update”按钮
    $('#updateCustomerConfirmButton_{{ kehu.id }}').click(function(event) {
      event.preventDefault();
    // 阻止默认的提交行为（因为我们在AJAX中提交表单）
    event.preventDefault();
    // 获取表单数据并构建更新对象
    var formData = $('#updateCustomerForm_{{ kehu.id }}').serializeArray();
    var updateData = {};
    $.each(formData, function(index, field) {
      updateData[field.name] = field.value;
    });
    // 添加CSRF令牌（确保你的Django视图需要CSRF保护）
    updateData['csrfmiddlewaretoken'] = $('input[name=csrfmiddlewaretoken]').val();

    // 发送POST请求发送POST请求更新客户资料
    $.ajax({
      url: '/' + {{ kehu.id }}  + '/gonghai_zt/', // 确保此处的customer.id能正确替换
      type: 'POST',
      data: updateData,
      success: function(response) {
            // 在这里处理响应结果
          if (response.status === 'success') {
                $('#message_{{ kehu.id }}').html(response.message).removeClass('fade').addClass('show');
        // 成功提示并关闭模态框
            setTimeout(function() {
            $('#message_{{ kehu.id }}').alert('close');
        }, 4000);
            setTimeout(() => {
    $('#updateCustomerModal_{{ kehu.id }}').modal('hide');
}, 1000);
          // 刷新页面
             location.reload();
            }
      },
      error: function(xhr, status, error) {
    // 在这里处理请求错误
    console.error("请求失败: " + error);
    // 在 #message div 中显示错误消息
    $('#message_{{ kehu.id }}').html("请求失败: " + error);
}


    });
  });

});

    </script>

</td>
